package activity.personal;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.hutoubenjg.R;

import java.util.Observable;
import java.util.Observer;

import base.BaseActivity;
import bean.login_bean.PersonDataBean;
import presenter.UserPresenter;
import util.utls.ByAlert;
import util.utls.Const;
import util.utls.IsUtilUid;
import util.utls.SPUtils;
import util.volley.ResultEntity;

/**
 * 通过旧密码修改密码
 */
public class ActivityJiDeUserPass extends BaseActivity implements Observer {
    TextView tvTitle;
    ImageView back;
    EditText old_pass, new_pass, new_pass02;
    TextView pass_ok;

//    UserPresenter userPresenter;

    String phonenumber;
    String newPass;
    UserPresenter userPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ji_de_pass);
        tvTitle.setText("修改密码");
        userPresenter = new UserPresenter(this);
    }

    @Override
    protected void onInit() {

    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.pass_ok:
                String oldPass = old_pass.getText().toString();
                newPass = new_pass.getText().toString();
                String newPass02 = new_pass02.getText().toString();
                if (oldPass.equals("")) {
                    ByAlert.alert("请输入旧密码");
                    return;
                }
                if (newPass.equals("")) {
                    ByAlert.alert("请输入新密码密码");
                    return;
                }
                if (newPass02.equals("")) {
                    ByAlert.alert("请再次输入新密码");
                    return;
                }
                if (!newPass.equals(newPass02)) {
                    ByAlert.alert("两次输入不一致");
                    return;
                }
                String uid = SPUtils.get(this, null, Const.ID, "") + "";
                showProgressDialog("请稍后", true);
                userPresenter.updatePwdByPwd(uid, oldPass, newPass);
                break;

            default:
                break;
        }
    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity = handlerError(data);
        if (entity != null) {
            IsUtilUid.tianjia(entity);
            if (entity.getEventType() == UserPresenter.updatepwdbypwd_success) {
                ByAlert.alert(entity.getData());
                phonenumber=SPUtils.get(ActivityJiDeUserPass.this,null,Const.MOBILE,"")+"";
                userPresenter.login(phonenumber, newPass);
            } else if (entity.getEventType() == UserPresenter.updatepwdbypwd_fail) {
                ByAlert.alert(entity.getData());
            } else if (entity.getEventType() == UserPresenter.login_success) {
                PersonDataBean bean = (PersonDataBean) entity.getData();
                if (bean != null) {
                    SPUtils.put(ActivityJiDeUserPass.this, null,
                            Const.PASSWORD, bean.getPassword());
                }
                finish();
            } else if (entity.getEventType() == UserPresenter.login_fail) {
                ByAlert.alert(entity.getData());
            }
        }
        try {
            closeProgressDialog();
        } catch (Exception e) {

        }
    }
}
